* Potholes, 311 Reports, and a theory of heterogenous resident demand for city services
* Cook, Zuhlke, Saywitz

* this file replicates the figures and findings in the main paper and appendix of Cook, Zuhlke, and Saywitz (2024) PSJ paper. 
* note, the maps in the main paper and Appendix are produced in a separate .do file: maps - final - replication materials - 2142024.do

* last updated: February 14, 2024
* last updated by: Sam 

* files needed: 
*** PSJ_RR_Data_10252023.csv

************************************************
* PRELIMINARIES


version 16 

cd "C:\Users\szuhlke\OneDrive - University of Iowa\Desktop\Stata Workspace\potholes"

import delimited "PSJ_RR_Data_10252023.csv", clear


* formatting for figures in paper
set scheme s1mono

* install necessary packages - these need to be installed in order to export the tables for the paper and to create graphics

*ssc install estout  // for exporting tables
*ssc install coefplot // for coefficient plots

* encode district variable
encode district, generate(district_num)


***********************************************
* APPENDIX: Figures 1a and 1b

* appendix figure 1a
histogram x311_call_count_sum, frequency plotregion(style(none)) xtitle("311 Reports about Potholes")
graph export "hist-311calls-fulldata.jpg", replace

* appendix figure 1b
histogram proactive_num_filled_sum, frequency plotregion(style(none)) xtitle("Proactively Filled Potholes")
graph export "hist-potholes-fulldata.jpg", replace


************************************************

* MODELING - MAIN PAPER 

* generate labels for variables 
label variable x311_call_count_sum "311 Reports"
label variable proactive_num_filled_sum "Potholes"
label variable asinh_proactive_num_filled_sum "Potholes"
label variable ses_r "SES"
label variable per_black_z "% Black"
label variable per_hisp_z "% Hispanic"
label variable per_asian_z "% Asian"
label variable pop_tot_z "Total Population"
label variable district_num "District"
label variable road_length_z2 "Total Road Length"
label variable year "Year"

* table 1, model 1: count (poisson), dv: potholes 

eststo m1: poisson proactive_num_filled_sum per_black_z per_hisp_z per_asian_z ses_r road_length_z2 pop_tot_z i.district_num i.year, robust 

* generate and store m1 marginal effects
eststo margins_m1: margins, dydx(*)

* table 1, model 2: count (poisson), dv: 311 reports

eststo m2:  poisson x311_call_count_sum asinh_proactive_num_filled_sum per_black_z per_hisp_z per_asian_z ses_r road_length_z2 pop_tot_z i.district_num i.year, robust 

* generate and store m2 marginal effects
eststo margins_m2: margins, dydx(*)
	
* table 1, model 3: interactive count (poisson), dv: 311 reports 

eststo m3:  poisson x311_call_count_sum asinh_proactive_num_filled_sum ///
	c.per_black_z##c.ses_r  c.per_hisp_z##c.ses_r ///
	per_asian_z road_length_z2 pop_tot_z i.district_num i.year, robust 

* generate and store m3 marginal effects
eststo margins_m3: margins, dydx(*)

* export table of estimates (Table 1 in main paper )
	esttab m1 m2 m3 using estimates-all-rr-oct23.tex, ///
		nogap b(3) se(3) starlevels(* .10 ** .05 *** .01) aic ///
		title("") label compress replace 
		
		* fixed effects estimates from all these models are displayed in the Appendix, Table 2
	
***************************************************

* FIGURES - MAIN PAPER 

* re-estimate m1, estimates used to generate Figure 5 
poisson proactive_num_filled_sum per_black_z per_hisp_z per_asian_z ses_r road_length_z2 pop_tot_z i.district_num i.year, robust 

* Figure 5a: % Black 
margins, at(per_black_z=(-1(1)3))
marginsplot, title("") ytitle("Predicted Number of Potholes") xtitle("% Black (standardized)") plotregion(style(empty))
graph export "rr_h1-potholes-black.jpg", replace

* Figure 5b: % Hispanic 
margins, at(per_hisp_z=(-2(1)2))
marginsplot, title("") ytitle("Predicted Number of Potholes") xtitle("% Hispanic (standardized)") plotregion(style(empty))
graph export "rr_h1-potholes-hispanic.jpg", replace

* Figure 5c: SES 
margins, at(ses_r=(-2(1)3))
marginsplot, title("") ytitle("Predicted Number of Potholes") xtitle("Socioeconomic Status (standardized)") plotregion(style(empty))
graph export "rr_h2-potholes-ses.jpg", replace

* re-estimate m2, estimates used to generate Figures 6 and 7 
poisson x311_call_count_sum asinh_proactive_num_filled_sum per_black_z per_hisp_z per_asian_z ses_r road_length_z2 pop_tot_z i.district_num i.year, robust 

* Figure 6
margins, at(asinh_proactive_num_filled_sum=(0(1)8))
marginsplot, title("") ytitle("Predicted Number of 311 Reports") xtitle("Potholes (transformed)") plotregion(style(empty))
graph export "rr_h3-311-potholes.jpg", replace

* Figure 7a: % Black 
margins, at(per_black_z=(-1(1)3))
marginsplot, title("") ytitle("Predicted Number of 311 Reports") xtitle("% Black (standardized)") plotregion(style(empty))
graph export "rr_h5-311-black.jpg", replace

* Figure 7b: % Hispanic 
margins, at(per_hisp_z=(-2(1)2))
marginsplot, title("") ytitle("Predicted Number of 311 Reports") xtitle("% Hispanic (standardized)") plotregion(style(empty))
graph export "rr_h5-311-hispanic.jpg", replace

* Figure 7c: SES 
margins, at(ses_r=(-2(1)3))
marginsplot, title("") ytitle("Predicted Number of 311 Reports") xtitle("Socioeconomic Status (standardized)") plotregion(style(empty))
graph export "rr_h4-311-ses.jpg", replace


* re-estimate m3, estimates used to generate Figure 8 
poisson x311_call_count_sum asinh_proactive_num_filled_sum ///
	c.per_black_z##c.ses_r  c.per_hisp_z##c.ses_r ///
	per_asian_z road_length_z2 pop_tot_z i.district_num i.year, robust 
	

* Figure 8a: % Black x SES 
margins, dydx(per_black_z) at(ses_r=(-2(1)3))
marginsplot, ytitle("Effect of % Black on 311 Reports") title("") xtitle("Socioeconomic Status (standardized)") plotregion(style(empty)) level(95)
graph export "rr_h7-int-black.jpg", replace


* Figure 8b: counterfactual results - % Black x SES
	margins,  at(per_black_z==1 ses_r==(-2(1)3)) at(per_black_z==0 ses_r==(-2(1)3)) at(per_black_z==-1 ses_r==(-2(1)3)) 
	marginsplot, plotregion(style(empty)) title("") ytitle("Predicted 311 Reports") xtitle("Socioeconomic Status (standardized)") legend(order(4 "+1std" 5 "Avg" 6 "-1std") rows(1) title("% Black", size(small)))
graph export "rr_h7-cf-black.jpg", replace



***************************************************
* FIGURES - APPENDIX 

* Figure 4: coefficient plot of average mfx - potholes (m1 in main paper)
coefplot (margins_m1, label(Model 1)), plotregion(style(none)) drop(*.district_num *.year _cons) xline(0)
graph export "rr_mfx_potholes.jpg", replace


* Figure 5: coefficient plot of average mfx - 311 report models (m2 and m3 in main paper)
coefplot (margins_m2, label(Model 2)) (margins_m3, label(Model 3)), plotregion(style(none)) drop(*.district_num *.year _cons) xline(0)
graph export "rr_mfx_311.jpg", replace



***************************************************
* APPENDIX: TABLE 1
* summary statistics 

summarize x311_call_count_sum proactive_num_filled_sum ses_r per_black_mean per_hisp_mean per_asian_mean pop_tot_mean road_length_mean


	